import { defineStore } from 'pinia';
import { useI18n } from 'vue-i18n';
import { ElMessage } from 'element-plus';
import { useElementPlus } from '@/composables/useElementPlus';

export const useFactoryStore = defineStore('factoryInfo', {
  state: () => ({
    currentLanguage: 'zh-cn',
    elementSize: 'default',
    address: '浙江省温州市龙湾区滨海园区金海大道339号',
    contactPhone: '+86 577-8688 8888',
    contactEmail: 'sales@packagingexpert.com',
    productionLines: [
      {
        type: '十色高速凹版印刷机',
        speed: '300m/min',
        width: '1.6m',
        function: '支持水性油墨印刷，最小网点再现1%'
      },
      {
        type: '全自动高速制袋机',
        speed: '250pcs/min',
        width: '1.2m',
        function: '三边封/自立袋/拉链袋/吸嘴袋一体化成型'
      },
      {
        type: '干式复合机',
        speed: '180m/min',
        width: '2.0m', 
        function: '二液型聚氨酯胶水，残留溶剂≤2mg/m²'
      },
      {
        type: '高速分切机',
        speed: '600m/min',
        width: '2.2m',
        function: '自动张力控制，分切精度±0.1mm'
      }
    ],
    certifications: [
      { 
        name: 'ISO9001质量管理体系认证',
        logo: 'iso9001.png',
        validity: '2026-12-31'
      },
      {
        name: 'FDA食品接触材料认证',
        logo: 'fda.png',
        validity: '2027-06-30'  
      },
      {
        name: 'BRC全球包装标准认证',
        logo: 'brc.png',
        validity: '2025-09-30'
      }
    ],
    packagingMaterials: [
      {
        type: '铝箔复合材料',
        thickness: '7-12μm',
        barrierProperties: '氧气透过率≤0.5cm³/m²·day',
        applications: ['食品包装', '医药包装']
      },
      {
        type: 'PET/AL/PE',
        thickness: '80-150μm', 
        heatResistance: '121℃ 30min',
        applications: ['高温蒸煮袋', '宠物食品包装']
      }
    ],
    clientCases: [
      {
        name: '蒙牛乳业',
        solution: '长效保鲜铝箔立式袋',
        capacity: '月供200万只'
      },
      {
        name: '同仁堂药业',
        solution: '中药粉剂防潮复合膜包装',
        capacity: '年产量500吨' 
      }
    ]
  }),

  actions: {
    async fetchCertifications() {
      try {
        const response = await fetch('/api/certifications');
        this.certifications = await response.json();
      } catch (error) {
        ElMessage.error(useI18n().t('error.fetch_certifications'));
      }
    },
    updateElementSize(size) {
      this.elementSize = size;
      document.documentElement.style.fontSize = size === 'large' ? '16px' : '14px';
    },
    async switchLanguage(lang) {
      this.currentLanguage = lang;
      localStorage.setItem('preferred-language', lang);
      const { updateLocale } = useElementPlus();
      await updateLocale(lang);
      window.location.reload();
    }
  },

  getters: {
    formattedCertifications: (state) => state.certifications.map(cert => ({
      ...cert,
      validityDate: new Date(cert.validity).toLocaleDateString()
    })),
    getQualityMetrics: (state) => ({
      defectRate: '≤0.2%',
      sealStrength: '≥50N/15mm',
      migrationTest: '符合GB 31604.1-2015标准'
    }),
    
    getMonitoringData: (state) => ({
      temperature: '23±2℃',
      humidity: '45±5%RH',
      equipmentStatus: '运行中',
      realtimeOutput: '1520m/h'
    }),
    
    getProductionSchedule: (state) => ({
      timestamp: new Date().toLocaleString(state.currentLanguage.replace('-', '_'), { 
        timeZone: 'Asia/Shanghai',
        hour12: state.currentLanguage.startsWith('en') 
      }),
      currentOrders: {
        actual: 48,
        capacity: state.productionLines.length * 12500
      },
      productionMetrics: {
        deliveryRate: '99.2%',
        taktTime: (8 * 60 / 15000).toFixed(2) + '分钟/件',
        dayShift: '82%',
        nightShift: '78%',
        overall: '80%'
      },
      equipmentEfficiency: {
        changeovers: 8,
        oee: '88.6%',
        utilization: { printing: '92%', compounding: '95%', slitting: '89%' }
      },
      planningParams: {
        minOrder: '50,000pcs',
        leadTime: useI18n().t('store.lead_time'),
        schedulingAccuracy: '98.72%'
      }
    }),
    
    getEnvironmentalData: (state) => ({
      energyConsumption: '0.8kWh/千件',
      wasteRecyclingRate: '96.5%',
      vocEmission: '≤20mg/m³',
      environmentalCert: '通过ISO14001认证'
    })
  }
});
